home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / disk-man / mtools-3.000 / mtools-3 / mtools-3.0 / subdir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-30  |  2.1 KB  |  119 lines

  1. #include "sysincludes.h"
  2. #include "msdos.h"
  3. #include "mtools.h"
  4. #include "vfat.h"
  5. #include "file.h"
  6. #include "buffer.h"
  7.  
  8. /*
  9.  * Find the directory and load a new dir_chain[].  A null directory
  10.  * is OK.  Returns a 1 on error.
  11.  */
  12.  
  13.  
  14. static void bufferize(Stream_t **Dir)
  15. {
  16.     Stream_t *BDir;
  17.  
  18. #if 1    
  19.     if(!*Dir)
  20.         return;
  21.     BDir = buf_init(*Dir, 16384, MDIR_SIZE, MDIR_SIZE);
  22.     if(!BDir){
  23.         FREE(Dir);
  24.         *Dir = NULL;
  25.     } else
  26.         *Dir = BDir;
  27. #endif
  28. }
  29.     
  30.     
  31. Stream_t *descend(Stream_t *Dir, Stream_t *Fs, char *path, int barf,
  32.           char *outname)
  33. {
  34.     /* this function makes its own copy of the Next pointers */
  35.  
  36.     int entry;
  37.     struct directory dir;
  38.     Stream_t *SubDir;
  39.     int ret;
  40.  
  41.     if(path[0] == '\0' || !strcmp(path, "."))
  42.         /* don't waste timing scanning through the directory if
  43.          * we look for dot anyways */
  44.         return COPY(Dir);
  45.  
  46.     entry = 0;
  47.     ret = vfat_lookup(Dir, Fs, &dir, &entry, 0, path,
  48.               ACCEPT_DIR | SINGLE | DO_OPEN,
  49.               outname, 0, 0,
  50.               & SubDir);
  51.  
  52.     if(ret == 0){
  53.         bufferize(&SubDir);
  54.         return SubDir;
  55.     }
  56.  
  57.     /*
  58.      * If path is '..', but it wasn't found, then you must be
  59.      * at root.
  60.      */
  61.     if (!strcmp(path, "..")){
  62.         SubDir = open_root(COPY(Fs));
  63.         bufferize(&SubDir);
  64.         if(!SubDir)
  65.             FREE(&Fs);
  66.         return SubDir;
  67.     }
  68.     if (barf)
  69.         fprintf(stderr, "Path component \"%s\" is not a directory\n",
  70.             path);
  71.     return NULL;
  72. }
  73.  
  74.  
  75. /*
  76.  * Descends the directory tree.  Returns 1 on error.  Attempts to optimize by
  77.  * remembering the last path it parsed
  78.  */
  79. Stream_t *subdir(Stream_t *Fs, char *pathname)
  80. {
  81.     /* this function makes its own copy of the Next pointers */
  82.  
  83.     char *s, *tmp, tbuf[MAX_PATH], *path, terminator;
  84.     Stream_t *Dir, *NewDir;
  85.  
  86.     /* FIXME: path caching */
  87.  
  88.     strcpy(tbuf, pathname);
  89.  
  90.     /* start at root */
  91.     Dir = open_root(COPY(Fs));
  92.     bufferize(&Dir);
  93.     if (!Dir){
  94.         FREE(&Fs);
  95.         return Dir;
  96.     }
  97.  
  98.     /* separate the parts */
  99.     tmp = &tbuf[1];
  100.     for (s = tmp; ; ++s) {
  101.         if (*s == '/' || *s == '\0') {
  102.             path = tmp;
  103.             terminator = *s;
  104.             *s = '\0';
  105.             if (s != tmp && strcmp(path,".")){
  106.                 NewDir = descend(Dir, Fs, path, 1, 0);
  107.                 FREE(&Dir);
  108.                 if(!NewDir)
  109.                     return NewDir;
  110.                 Dir = NewDir;
  111.             }
  112.             if (!terminator)
  113.                 break;
  114.             tmp = s + 1;
  115.         }
  116.     }
  117.     return Dir;
  118. }
  119.